using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBarUi : MonoBehaviour
{
    private Entity entity=>GetComponentInParent<Entity>();
    private RectTransform rectTransform;
    private Slider slider;
    private CharacterStart mystart=>GetComponentInParent<CharacterStart>();
    private void Start()
    {
        rectTransform= GetComponent<RectTransform>();
        slider=GetComponentInChildren<Slider>();
        UpdateHealthUI();
    }
    private void Update()
    {
        UpdateHealthUI();
    }
    private void FlipUI() => rectTransform.Rotate(0, 180, 0);
    private void OnDisable()
    {
        if(entity!=null)
            entity.onFlipped -= FlipUI;
        if(mystart!=null)
            mystart.onHealthChanged -= UpdateHealthUI;
    }
    private void UpdateHealthUI()
    {
        slider.maxValue = mystart.GetMaxHealthValue();
        slider.value = mystart.currentHealth;
    }
    private void OnEnable()
    {
        entity.onFlipped += FlipUI;
        mystart.onHealthChanged += UpdateHealthUI;
    }
}
